const express = require("express");
const admin = express.Router();
const axios = require('axios')
const qs = require('qs')

var headers = {
	"Host": "cloud.cn2030.com",
	"Content-Type": "application/json",
	"Accept": "*/*",
	"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 11_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E217 MicroMessenger/6.8.0(0x16080000) NetType/WIFI Language/en Branch/Br_trunk MiniProgramEnv/Mac",
	"Referer": "https://servicewechat.com/wx2c7f0f3c30d99445/91/page-frame.html",
	"Accept-Language": "zh-cn",
	"Cookie": "ASP.NET_SessionId=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2MzY2ODY1MjcuMTI5ODM3MywiZXhwIjoxNjM2NjkwMTI3LjEyOTgzNzMsInN1YiI6IllOVy5WSVAiLCJqdGkiOiIyMDIxMTExMjExMDg0NyIsIlVzZXJpZCI6NjgwNzE3NywiQ2F0IjoyLCJLZXkiOiJjYjcyNmFiZjRlMTJjMWFmIiwiT3BlbmlkIjoib3FyNW81RjdfXzlVdVBobHIwRW02cnp3SU5HSSIsIklzYmxhY2siOmZhbHNlLCJVbmlvbmlkIjoib1UyNlh0MnZQRHJPR2k1V1FIMlpRMml5N2I0ayIsIklwIjoiMjE4Ljc5LjYwLjExMSIsIkd1aWQiOm51bGwsIk14aWQiOm51bGwsImlnbm9yZSI6ZmFsc2UsIngiOjB9.0NOhPSX4U128dYIaX_qq9PBMBeJBhXB1L0u4kFx3_As"
}

var tableStyle = `<style type="text/css">
table.gridtable {
	font-family: verdana,arial,sans-serif;
	font-size:11px;
	color:#333333;
	border-width: 1px;
	border-color: #666666;
	border-collapse: collapse;
}
table.gridtable th {
	border-width: 1px;
	padding: 8px;
	border-style: solid;
	border-color: #666666;
	background-color: #dedede;
}
table.gridtable td {
	border-width: 1px;
	padding: 8px;
	border-style: solid;
	border-color: #666666;
	background-color: #ffffff;
}
</style>`


//获取 可供预约的诊所机构
admin.get('/index', (req, res) => {
	console.log('-----准备获取')
	const query = {
		act: 'CustomerList',
		city: JSON.stringify(["陕西省", "西安", ""]),
		lat: '31.223520278930664',
		lng: '121.4559097290039',
		id: '0',
		cityCode: '610100',
		product: '1' // 1 九价 2 四价 3 二价进口  54 二价（国产）
	}


	const URL = "https://cloud.cn2030.com/sc/wx/HandlerSubscribe.ashx";

	axios.get(`${ URL }?${ qs.stringify(query)}`, {
		headers
	}).then(result => {
		let data = result.data.list.filter(item => item.tags.length),
			sdata = result.data.list.filter(item => !item.tags.length),
			title = `<h1>可预约</h1><br/>`,
			stitle = `<br/><br/><h1>不可预约</h1><br/>`,
			str = '',
			sstr = '';

		data.map(item => {
			str = str +
				`<a style="color:#333" href="/jiujia/index/time/${item.id}" >${item.cname}</a><br/> <br/>`;
		})

		sdata.map(item => {
			sstr = sstr +
				`<a style="color:#333" href="/jiujia/index/time/${item.id}" >${item.cname}</a><br/> <br/>`;
		})

		res.send(title + str + stitle + sstr);
		console.log('------------结束------------')
	}).catch(err => {
		console.log(err)
	})

})

//获取 可供预约的诊所机构
admin.get('/index/info/:id', (req, res) => {
	console.log('-----准备获取', req.params)
	console.log('详情-----准备获取')
	const query = {
		act: 'CustomerProduct',
		lat: '31.223520278930664',
		lng: '121.4559097290039',
		id: req.params.id, //接种点 id 只需要改这个参数就行
	}
	const URL = "https://cloud.cn2030.com/sc/wx/HandlerSubscribe.ashx";

	axios.get(`${ URL }?${ qs.stringify(query)}`, {
		headers
	}).then(result => {
		let data = result.data,
			title = `<h1>${data.cname}</h1><br/>`,
			str = '';

		data.list.forEach(item => {
			str = str +
				`<a style="color:#333" href="/jiujia/index/time/${req.params.id}" >${item.text}</a><br/> <br/>`;

		})
		res.send(title + str);
		console.log('------------结束------------')
	}).catch(err => {
		console.log(err)
	})
})


//获取 可供预约的诊所机构
admin.get('/index/time/:id', (req, res) => {
	console.log('-----准备获取', req.params)
	console.log('详情-----准备获取')

	const query = {
		act: 'GetCustSubscribeDateAll',
		pid: 1,
		id: Number(req.params.id),
		month: 202111, //月份
	}

	const URL = "https://cloud.cn2030.com/sc/wx/HandlerSubscribe.ashx";

	axios.get(`${ URL }?${ qs.stringify(query)}`, {
		headers
	}).then(result => {
		let data = result.data.list,
			str = '',
			table = '';
		if (data && data.length) {
			//href="/jiujia/index/info/${req.params.id}"
			data.map(item => {
				str = str + `<tr>
					<td>${item.date}</td>
					<td><a>抢购</a></td>
				</tr>`
			})

			table = `
		<table class="gridtable">
			<tr>
				<th>可预约时间</th>
				<th>操作</th>
			</tr>
			${str}
		</table>`

			res.send(tableStyle + table);
		} else {
			res.send('没有时间可供预约');
		}

		console.log('------------结束------------')
	}).catch(err => {
		console.log(err)
	})
})




admin.get('*', (req, res) => {
	res.send('我是后台')
})
module.exports = admin;
